package ui.control
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import ui.GuiEnum;
	import ui.GuiWnd;

	/**
	 * 标签组合 
	 * @author Administrator
	 * 
	 */	
	public class GuiTab extends GuiWnd
	{
		private var m_btnList:Array = new Array();
		private var m_nIndex:int = -1;
		
		public function GuiTab()
		{
		}
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			if(child is GuiCheckBox)
			{
				addItem(child as GuiCheckBox);
			}
			return super.addChild(child);
		}
		
		public function addItem(btn:GuiCheckBox, index:int = -1):void
		{
			
			btn.addEventListener(MouseEvent.CLICK, onClick);
			
			if(index < 0)
			{
				m_btnList.push(btn);
			}
			else
			{
				m_btnList.splice(index,0,btn);
			}
			addChild(btn);
		}
		
		public function setItemText(index:int, str:String):void
		{
			var item:GuiCheckBox = m_btnList[index] as GuiCheckBox;
			if(item)
			{
				item.text = str;
			}
		}
		
		public function getItem(index:int):GuiCheckBox
		{
			return m_btnList[index] as GuiCheckBox;
		}
		
		public function showItem(index:int, b:Boolean):void
		{
			var item:GuiCheckBox = m_btnList[index] as GuiCheckBox;
			if(item)
			{
				item.visible = b;
			}
		}
		
		public function deleteItem(index:int):void
		{

		}
		
		protected function onClick(event:MouseEvent):void
		{
			this.selectedIndex = this.m_btnList.indexOf(GuiCheckBox(event.currentTarget));
		}
		
		public function get nSelectedIndex():int
		{
			return m_nIndex;
		}
		
		public function set selectedIndex(nIndex:int):void
		{
			//上一个角禁
			SetEnbale(m_nIndex, true);
			
			m_nIndex = nIndex;
			
			//禁用当前标签的鼠标消息
			SetEnbale(m_nIndex, false);
			
			this.dispatchEvent(new Event(GuiEnum.TAB_CHANGE));
		}
		
		private function SetEnbale(index:int,bEnbale:Boolean):void
		{
			var btn:GuiCheckBox = m_btnList[index] as GuiCheckBox;
			
			if(btn)
			{
				btn.isDown = bEnbale ? false : true;
				btn.mouseEnabled = bEnbale;
				btn.mouseChildren = bEnbale;
			}
		}
		
		
		public function get nItemNum():int
		{
			return m_btnList.length;
		}
		
		public function ClearItem():void
		{
			for (var i:int = 0; i < m_btnList.length; i++) 
			{
				var item:GuiCheckBox = m_btnList[i] as GuiCheckBox;
				item.destroy();
				item = null;
			}
		}
	}
}